﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SmartSpace.Cwt;

namespace WebApp._Actions
{
    /// <summary>
    /// Summary description for SubmitContact
    /// </summary>
    public class SubmitContact : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string name = context.Request.Form["customer_username"];
            string telephone = context.Request.Form["customer_telephone"];
            string email = context.Request.Form["customer_email"];
            string message = context.Request.Form["customer_message"];
            string verifycode = context.Request.Form["verify_code"];
            string validateVerifycode = context.Request.Cookies["verifycode"].Value;

            context.Response.ContentType = "text/plain";
            if (verifycode != validateVerifycode)
            {
                context.Response.Write("{\"success\":\"false\", \"message\":\"提交失败,验证码不正确!\"}");
            }
            else
            {
                Contact contact = new Contact();
                contact.Id = Guid.NewGuid();
                contact.Name = name;
                contact.Telephone = telephone;
                contact.Email = email;
                contact.Message = message;

                ICwtService service = Global.Get<ICwtService>();
                if (service.SaveContact(contact))
                {
                    context.Response.Write("{\"success\":\"true\", \"message\":\"提交成功!\"}");
                }
                else
                {
                    context.Response.Write("{\"success\":\"false\", \"message\":\"提交失败!\"}");
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}